package com.example.anew.journey.mynewjourney.firstchangelogic.FirstChangeSetting.persenter;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.example.anew.journey.mynewjourney.R;
import com.example.anew.journey.mynewjourney.base.MyApplication;
import com.example.anew.journey.mynewjourney.bean.TakeDateBean;
import com.example.anew.journey.mynewjourney.config.RBConstants;
import com.example.anew.journey.mynewjourney.firstchangelogic.FirstChangeSetting.bean.logOutBean;
import com.example.anew.journey.mynewjourney.firstchangelogic.FirstChangeSetting.model.FirstChangeSettingModel;
import com.example.anew.journey.mynewjourney.firstchangelogic.FirstChangeSetting.model.ISFirstChangeSettingModel;
import com.example.anew.journey.mynewjourney.firstchangelogic.FirstChangeSetting.view.ISFirstChangeSettingView;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import okhttp3.Call;
import okhttp3.Response;

/**
 * 杭州惟君医疗科技有限公司13
 * Created by 20433 on 2018/3/16
 */

public class FirstChangeSettingPersenter {
    private ISFirstChangeSettingView mISFirstChangeSettingView;
    private PopupWindow mPopWindowClearCache;
    private ISFirstChangeSettingModel mISFirstChangeSettingModel;

    public FirstChangeSettingPersenter(ISFirstChangeSettingView iSFirstChangeSettingView) {
        this.mISFirstChangeSettingView = iSFirstChangeSettingView;
        mISFirstChangeSettingModel = new FirstChangeSettingModel();

    }

    //点击 绑定账号
    public void onClickAccountBinding() {
        mISFirstChangeSettingView.toAccountBindingPager();
    }

    //点击 修改密码
    public void onClickChangePassword() {
        mISFirstChangeSettingView.toChangePasswordPager();
    }

    //点击 关于手机
    public void onClickAboutApp() {
        mISFirstChangeSettingView.toAboutAppPager();
    }

    //点击 清除缓存
    public void onClickClearCache(Context mcontext) {
        View contentView = LayoutInflater.from(mcontext).inflate(R.layout.clear_cache_popu, null);
        mPopWindowClearCache = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        mPopWindowClearCache.setContentView(contentView);
        mISFirstChangeSettingView.findClearCacheView(contentView);
        //显示PopupWindow
        View rootview = LayoutInflater.from(mcontext).inflate(R.layout.activity_first_change_personal_information, null);
        mPopWindowClearCache.showAtLocation(rootview, Gravity.CENTER, 0, 0);
    }

    //点击 新消息通知
    public void onClickNewMessager() {
        mISFirstChangeSettingView.toNewMessagerPager();
    }

    //点击 意见反馈
    public void onClickFeedBack() {
        mISFirstChangeSettingView.toFeedBackPager();
    }

    //点击 退出登录
    public void onClicklogOut(final Context mContext) {
       // mISFirstChangeSettingView.logOut();
        if (getUserID(mContext) == null) return;
        String url = RBConstants.SERVER_URL+RBConstants.PULL_OUT;
        OkHttpUtils
                .post()
                .url(url)
                .addParams("cus_id", getUserID(mContext).getUser_id())
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String NetworkResponseDate = response.body().string();
                        Gson gson = new Gson();
                        // NLogger.e("TAG", "===main=========NetworkResponseDate=================" + NetworkResponseDate);
                        final logOutBean logOutBean =   gson.fromJson(NetworkResponseDate, logOutBean.class);
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(logOutBean.errorCode == 200){
                                    mISFirstChangeSettingView.showToastInformation("退出成功");
                                    mISFirstChangeSettingView.logOut();
                                    mISFirstChangeSettingModel.clear(mContext);
                                }else{
                                    mISFirstChangeSettingView.showToastInformation("退出失败");
                                }
                            }
                        });


                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }


    //点击 清除缓存 取消
    public void onClickClearCacheCancel() {
        mPopWindowClearCache.dismiss();
    }

    //点击 清除缓存 确认
    public void onClickClearCacheConfirm() {
        onClickClearCacheCancel();
    }

    private TakeDateBean getUserID(Context mContext) {
        TakeDateBean bean = mISFirstChangeSettingModel.TakeDate(mContext);
        if(bean == null) return null;
        return bean;
    }
}
